<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri='http://www.springframework.org/security/tags' prefix='sec'%>
<!DOCTYPE html>
<html>
	<head>
		<jsp:include page="../../inc/inc.jsp"></jsp:include>
		<script type="text/javascript">
			$(document).ready(function () { 
				$("#dept_t").tree({    
					url:'dept_tree',
					lines:true,
					type:'post',
					parentField : 'parentId',
					onSelect: function(node){
						selectId = node.id;
						selectParentId = node.parentId;
						var url = 'dept_detail?dept_id='+node.id;
						$("#deptDetail").attr('src', url);
					},
					onLoadSuccess:function(node,data){  
						$("#dept_t li:eq(0)").find("div").addClass("tree-node-selected");   //设置第一个节点高亮  
					 	var n = $("#dept_t").tree("getSelected");  
						if(n != null){  
					     	$("#dept_t").tree("select",n.target);    //相当于默认点击了一下第一个节点，执行onSelect方法  
					 	} 
					}
				});
				
				
				//所属部门
				$('#parentId').combotree({
					url : 'dept_tree',
					parentField : 'parentId',
					lines : true,
					panelHeight : 'auto',
					width : 200
				});
			})
		</script>
	</head>
	
	<body>
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'north',border:false" style="height:30px;overflow:hidden;background-color:#f4f4f4">
				<table>
					<tr>
						<td>
							<sec:authorize ifAnyGranted='B_ZZGL_ADD'>
								<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-addjg',plain:true" onclick="addDept();">添加机构</a>
							</sec:authorize>
						</td>
						<td>
							<sec:authorize ifAnyGranted='B_ZZGL_DEL'>
								<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="delDept();">删除机构</a>
							</sec:authorize>
						</td>
					</tr>
				</table>
			</div>
			<div data-options="region:'west',border:false,split:true" style="width:220px;padding:10px">
				<ul class="easyui-tree" id="dept_t"></ul>
			</div>
			<div data-options="region:'center',border:false" >
				<iframe id="deptDetail" scrolling="no" width="100%" height="99%" frameborder="0"></iframe>
			</div>
		</div>
		
		
		<div id="dlg" class="easyui-dialog" style="width:600px;height:auto;padding:10px 20px" buttons="#dlg-buttons" data-options="closed:true,modal:true">
			<form id="dept_form" method="post">
				<table class="grid">
					<tr>
						<td>部门名称：</td>
        				<td>
	             			<input type="text" name="deptName" class="easyui-textbox" data-options="required:true,validType:['length[0,30]']" />
        				</td>
        				<td>上级部门：</td>
        				<td>
	            			<input type="text" id="parentId" name="parentId" class="easyui-combotree" data-options="required:true" />
        				</td>
        			</tr>
        			<tr>
        				<td>部门类型：</td>
        				<td>
							<select name="deptType" class="easyui-combobox" data-options="width:100,editable:false,panelHeight:'auto'">
								<option value="0">部门</option>
								<option value="1">组</option>
							</select>
        				</td>
        				<td>部门主管：</td>
        				<td>
							<input class="easyui-combobox" id="deptManager" name="deptManager"/>
        				</td>
        			</tr>
        			<tr>
        				<td>部门描述：</td>
        				<td colspan="3">
							<input type="text" name="remark" class="easyui-textbox" data-options="multiline:true,height:50" style="width:300px;" />
        				</td>
        			</tr>
 				</table>
 			</form>
  		</div>
		<div id="dlg-buttons">
        	<a href="javascript:void(0)" class="easyui-linkbutton c8" data-options="iconCls:'icon-ok'" onclick="javascript:saveDept();" style="width:90px;">保存</a>
       	 	<a href="javascript:void(0)" class="easyui-linkbutton c2" data-options="iconCls:'icon-cancel'" onclick="javascript:$('#dlg').dialog('close');" style="width:90px;">取消</a>
    	</div>
	</body>
</html>

<script type="text/javascript">
	//打开添加部门
	function addDept() {
		$('#dept_form').form('clear');
		$('#dept_form').form('load',{
			parentId:selectId,
			deptType:0
		});
		$('#parentId').val(selectId);
		$('#dlg').dialog('open').dialog('setTitle','新增部门');
	}
	
	//保存
	function saveDept() {
		var data = form2Json("dept_form");
	    if($("#dept_form").form('validate')){
	        $.post('dept_save',{data:data},function(result) {
	            if(result.success){
	            	$('#dlg').dialog('close');
	                $.messager.show({
	                    title: '提示',
	                    msg: result.msg
	                });
	                $('#dept_t').tree('reload');
		            $('#parentId').combotree('reload');
	            }else{
	                $.messager.show({
	                    title: '提示',
	                    msg: result.msg
	                });
	            }
	        },'json');
	    }
	}
	
	//删除
	function delDept() {
		if(selectParentId == 'root_id') {
			$.messager.alert('警告','此组织节点不能删除','info');    
		} else if (selectId != '') {
			$.messager.confirm('提示','请确认是否删除?',function(r){
				if(r) {
					$.ajax({
						url:'dept_del',
						type:'post',
						data:{id:selectId},
						dataType:'json',
						success: function(result){
							if(result.success) {
								$.messager.show({
									title: '提示',
									msg: result.msg
								});
								//删除树中的结点
				                var node = $('#dept_t').tree('find', selectId);
				                $('#dept_t').tree('remove', node.target);
				               	//返回到父节点
				                var parentNode = $('#dept_t').tree('find', selectParentId);
				                $('#dept_t').tree('select', parentNode.target);
							} else {
								if(result.msg=='') {
									$.messager.alert('提示','该部门下有子部门,请先删除子部门','info');
								} else {
									$.messager.show({
										title: '提示',
										msg: result.msg
									});
								}
							}
						}
					});
				}
			});
		} else {
			$.messager.alert('提示','请选择组织机构','info');
		}
	}
</script>